<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\helper\Time;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class TraceRecord extends Backend
{
    
    /**
     * TraceRecord模型对象
     * @var \app\admin\model\TraceRecord
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\TraceRecord;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function searc()
    {

        $data = json_decode($this -> request -> get('filter'),true);
        
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = 0;
            $list = [];
            if(isset($data['sid']) && $data['exttime'] && $data['create_time']){
                extract($data);
                $timeArr = explode(' - ',$create_time);
                $timeBetween = [strtotime($timeArr[0]),strtotime($timeArr[1])];
                $positions = Db::name('trace_record') -> where(['sid' => $sid,'create_time' => ['between',$timeBetween]]) -> buildSql();
                $list = Db::name('trace_record')
                    -> alias('a')
                    -> join($positions.' b','b.position_id=a.position_id and a.create_time between b.create_time and b.create_time+'.$exttime)
                    -> where(['a.sid'=>['<>',$sid]])
                    -> field('a.*')
                    -> limit($offset, $limit)
                    -> select();
                
                $total = Db::name('trace_record')
                    -> alias('a')
                    -> join($positions.' b','b.position_number=a.position_number and a.create_time between b.create_time and b.create_time+'.$exttime)
                    -> where(['a.sid'=>['<>',$sid]])
                    -> field('a.*')
                    -> count();
            }



            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $today = Time::today();
        $todayStr = date("Y-m-d H:i:s",$today[0]).' - '.date("Y-m-d H:i:s",$today[1]);
        $this -> assign('today', $todayStr);
        return $this->view->fetch();
    }

}
